<?php
/**
 * Ohlasky Model for Ohlasky Component
 *
 * @license		GNU/GPL
 */

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.model');

/**
 * Ohlasky Ohlasky Model
 */
class OhlaskysModelOhlasky extends JModel {
/**
 * Constructor that retrieves the ID from the request
 *
 * @access	public
 * @return	void
 */
    function __construct() {
        parent::__construct();

        $array = JRequest::getVar('cid',  0, '', 'array');
        $this->setId((int)$array[0]);

    }

    /**
     * Method to set the ohlasky identifier
     *
     * @access	public
     * @param	int Ohlasky identifier
     * @return	void
     */
    function setId($id) {
    // Set id and wipe data
        $this->_id		= $id;
        $this->_data	= null;
    }

    function getDatum_form() {
        $datum = JRequest::getVar('datum',  '', '', 'string');
        return $datum;
    }

    function getMisto_id_form() {
        $misto_id = JRequest::getVar('misto_id',  '', '', 'integer');
        return $misto_id;
    }

    function getNazevMista(&$misto_id) {
        $nazev_mista = '';
        if (!empty($misto_id)) {
            $query = ' SELECT * FROM #__mista '.
                '  WHERE id = '.$misto_id;
            $this->_db->setQuery( $query );
            $misto_obj = $this->_db->loadObject();
            if (is_object($misto_obj)) {
                $nazev_mista = $misto_obj->misto;
            }
        }
        return $nazev_mista;
    }

    function getTyden_id_form() {
        $tyden_id = JRequest::getVar('tyden_id',  '', '', 'integer');
        return $tyden_id;
    }

    //    function getTydenOdeDne(&$tyden_id) {
    //        $ode_dne = '';
    //        if (!emtpy($misto_id)) {
    //            $query = ' SELECT * FROM #__tydny '.
    //                '  WHERE id = '.$tyden_id;
    //            $this->_db->setQuery( $query );
    //            $tyden_obj = $this->_db->loadObject();
    //            if (is_object($tyden_obj)) {
    //                $ode_dne = $tyden_obj->ode_dne;
    //                $ode_dne_obj = date_create($ode_dne);
    //                if (is_object($ode_dne_obj)) {
    //                    $ode_dne = $ode_dne_obj->format('j.n.Y');
    //                }
    //            }
    //        }
    //        return $ode_dne;
    //    }

    /**
     * Method to get a ohlasky
     * @return object with data
     */
    function &getData() {
    // Load the data
        if (empty( $this->_data )) {
            $query = ' SELECT * FROM #__ohlasky '.
                '  WHERE id = '.$this->_id;
            $this->_db->setQuery( $query );
            $this->_data = $this->_db->loadObject();
            if (is_object($this->_data)) {
                $datum_obj = date_create($this->_data->datum);
                if (is_object($datum_obj)) {
                    $this->_data->datum_str = $datum_obj->format('j.n.Y');
                } else {
                    $this->_data->datum_str = '';
                }
                $this->_data->misto_nazev = $this->getNazevMista($this->_data->misto_id);
            }
        }
        if (!$this->_data) {
            $this->_data = new stdClass();
            $this->_data->id = 0;
            $this->_data->cas = null;
            $this->_data->cas_konec = null;
            $this->_data->hruby_popis = null;
            $this->_data->datum = $this->getDatum_form();
            $this->_data->misto_id = $this->getMisto_id_form();
            $this->_data->misto_nazev = $this->getNazevMista($this->_data->misto_id);
            $this->_data->tyden_id = $this->getTyden_id_form();
            $datum_obj = date_create($this->_data->datum);
            if (is_object($datum_obj)) {
                $this->_data->datum_str = $datum_obj->format('j.n.Y');
            } else {
                $this->_data->datum_str = '';
            }
            $this->_data->zvyrazneni = 0;
        }
        return $this->_data;
    }

    /**
     * Method to store a record
     *
     * @access	public
     * @return	boolean	True on success
     */
    function store() {
        $row =& $this->getTable();
        $data = JRequest::get( 'post' );


        // Bind the form fields to the ohlasky table
        if (!$row->bind($data)) {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }

        // Make sure the ohlasky record is valid
        if (!$row->check()) {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }

        
        // Store the web link table to the database
        if (!$row->store()) {
            $this->setError( $row->getError() );
            return false;
        }
        return true;
    }

    /**
     * Method to delete record(s)
     *
     * @access	public
     * @return	boolean	True on success
     */
    function delete() {
        $id = JRequest::getVar( 'id', 0, '', 'integer' );
        $row =& $this->getTable();
        if (!$row->delete( $id )) {
            $this->setError( $row->getErrorMsg() );
            return false;
        }
        return true;
    }

}

